<?php

namespace app\admin\controller;
use app\adminmall\model\AgentModel;
use app\web\model\ChainStoreModel;
use app\common\model\Member;
use app\common\controller\AdminBaseController;
use think\Db;
/**
 * 商家列表
 */
class AgentController extends AdminBaseController
{
 	 protected $model;
	
    protected function _initialize() {
        parent::_initialize();
        $this->model = new AgentModel();
		$this->chainstoremodel = new ChainStoreModel();
		$tralist  = Db::name('mall_tradetype')->select();
        $this->assign('tralist', $tralist);
    }
    /**
     * 商家列表
     * @author [lx] 
     */
    public function index( $page = 1){
    	$map = [];
		if(request()->isPost()){
		 	$param = input('post.');
			//商户名称
			if($param['store_name']){
				$map['store_name'] = ['like', '%'.$param['store_name'].'%'];
			}
			if($param['store_tel']){
				$map['store_tel'] = ['like', '%'.$param['store_tel'].'%'];
			}
			if($param['province']){
				$map['province'] = $param['province'];
			}
			if($param['city']){
				$map['city'] = $param['city'];
			}
			if($param['district']){
				$map['district'] = $param['district'];
			}
	 	}
 		$list = db("mall_agent")->alias("a")->join("mall_tradetype t","t.id=a.industry_id ")->field("a.*,t.title")->where($map)->paginate(20);

        return $this->fetch('index', ['list' => $list]);
    }

    /**
     * 店铺信息
     * @author [longx] 
     */
    public function agentinfo($id){
		$shareUrl = "http://www.auth.cn/web/agent/index.html";
		$agent['shareUrl'] = 'http://www.vshop88.net/code/code.php?url='.urlencode($shareUrl).'&margin=1&size=10';
        $this->assign('ad',$this->model->find($id));
		$this->assign('code_url',$agent['shareUrl']);
		$this->assign('url',$shareUrl);
        return $this->fetch();
    }


    /**
     * 
     *添加
     * @author [lx] 
     */
    public function add()
    {
        if(request()->isAjax()){
        	$param=input('post.');
			$mem['mobile']=input("store_tel");
			$mem['password']=md5_salt("123456");
			$member=db("member")->where("mobile",input("store_tel"))->find();
			if(!$member){
				db("member")->insert($mem);
			}
			if ($this->model->allowField(true)->save($param) !== false) {
                $this->success('添加成功', url('Agent/index'));
            } else {
                $this->error('添加失败');
            }
        }
		$trades=cache("trades");//行业分类
		if(!$trades){
			$trades=db("mall_tradetype")->select();
		}
		$this->assign('trades',$trades);
        return $this->fetch();
    }

    /**
     * [edit 编辑]
     * @return [type] [description]
     * @author [lx]
     */
    public function edit()
    {
        if(request()->isAjax()){
            $param = input('post.');      
			$id=$param['id'];
			$result= $this->model->allowField(true)->save($param,$id);   
         	if ($result) {
                    $this->success('修改成功', url('Agent/index'));
            } else {
                $this->error('修改失败');
            }
        }
		$trades=db("mall_tradetype")->select();
        $id = input('param.id');
		 $this->assign('trades',$trades);
        $this->assign('ad',$this->model->find($id));
        return $this->fetch();
    }

    /**
     * [del删除]
     * @author [lx] 
     */
    public function delete()
    {
        $id = input('param.id');
        if ($this->model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
	
    /**
     * 商户类型变更
     * @param $id
     * @return mixed
     */
    public function examine($id) {
    	if(request()->isAjax()){
			if ($this->model->allowField(true)->save(['agent_type'=>'2'],['id'=>$id]) !== false) {
	        	$this->success('操作成功');
	        } else {
	            $this->error('操作失败');
	        }
		}
	}
	
	 /**
     * 审核状态变更
     * @param $id
     * @return mixed
     */
    public function toexamine($id) {
    	if(request()->isAjax()){
			if ($this->model->allowField(true)->save(['status'=>'1'],['id'=>$id]) !== false) {
	        	$this->success('操作成功');
	        } else {
	            $this->error('操作失败');
	        }
		}
	}
	
	/**
     * 冻结状态变更  1 正常  2 冻结
     * @param $id
     * @return mixed
     */
    public function frozen($id,$status) {
    	if(request()->isAjax()){
    		$sta = '';
    		if($status == 1){
    			$sta = '2';
    		}else if($status == 2){
    			$sta = '1';
    		}
			if ($this->model->allowField(true)->save(['store_state'=>$sta],['id'=>$id]) !== false) {
	        	$this->success('操作成功');
	        } else {
	            $this->error('操作失败');
	        }
		}
	}
	
	/**
     * 是否分销
     * @param int    $page
     * @return mixed
     */
    public function distribution($id,$integral) {
    	$distribution = $integral ? true : false;
		if ($this->model->allowField(true)->save(['is_distribution'=>$distribution],['id'=>$id]) !== false) {
        	$this->success("操作成功");
        } else {
            $this->error('操作失败');
        }
    }
	
	/**
     * 充值余额
     * @param int    $page
     * @return mixed
     */
    public function credit($id,$integral) {
	    $a='';
   		$member=$this->model->find($id);
   		$a=$member['store_balance']+$integral;
		if ($this->model->allowField(true)->save(['store_balance'=>$a],['id'=>$id]) !== false) {
        	$this->success("操作成功");
        } else {
            $this->error('操作失败');
        }
    }
    /**
     * 商家充值
     * @param int    $page
     * @return mixed
     */
    public function chongzhi() {
    	$member= new Member();
    	if(input("type")==2){//积分
			$message='积分充值';
			$member->insscoch(input("id"), $message, input("value"), 6);
    	}else{//余额
			$message="余额充值积分充值";
			$member->insbalch(input("id"), $message, input("value"), 6);
    	}
        return json(["code"=>200,'msg'=>'充值成功']);
       
    }
}